package be.kuleuven.cs.mop.gui.dialogs;


import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import be.kuleuven.cs.mop.app.controllers.Controllers;



/**
 * Represents a {@link Dialog} which provides {@link ActionListener} functionality
 */
public abstract class ActionDialog extends Dialog implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	private Controllers controllers;
	
	
	public ActionDialog(final Frame parent, final String title,
			final Controllers controllers) {
		super(parent, title);
		setControllers(controllers);
	}
	
	
	
	@Override
	public void actionPerformed(final ActionEvent event) {
		final Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					handle(event);
				} catch (final Exception e) {
					log(e);
				}
			}
		});
		t.setDaemon(true);
		t.start();
	}
	
	protected Controllers getControllers() { return controllers; }
	
	protected abstract void handle(ActionEvent event) throws Exception;
	
	private void setControllers(final Controllers controllers) {
		if (controllers == null)
			throw new NullPointerException("Controllers == NULL");
		
		this.controllers = controllers;
	}
	
}